// index.js
// 获取应用实例
const app = getApp()

Page({
  data: {
    username: ""
  },
  onLoad() {

  },
  add() {
    const _this = this;
    if (!_this.data.username) {
      wx.showModal({
        title: '提示',
        content: '请先登录',
        success(res) {
          if (res.confirm) {
            wx.getUserProfile({
              desc: 'desc',
              success: function (res) {
                _this.setData({ username: res.userInfo.nickName })
                wx.setStorage({
                  key: "username",
                  data: res.userInfo.nickName
                })
                wx.redirectTo({
                  url: '/pages/vote/vote',
                })
              }
            })
          } else if (res.cancel) {
          }
        }
      })
    } else {
      wx.redirectTo({
        url: '/pages/vote/vote',
      })
    }
  },
  onShow() {
    const _this = this;
    wx.getStorage({
      key: 'username',
      success: function (res) {
        _this.setData({ username: res.data })
      }
    })
  }
})